package org.fireflow2.designer.eclipse.properties.activity;

import org.eclipse.gef.editparts.AbstractEditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.ActivityWrapper;

public class ActivityAttributePropertySection extends AbstractPropertySection {
	AbstractEditPart editPart = null;
	AbsWrapper modelWrapper = null;
	Combo completeStrategyCombo = null;
	public void createControls(Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);

		Composite composite = getWidgetFactory()
				.createFlatFormComposite(parent);

		composite.setLayout(new GridLayout(2, false));
		
		// CompleteStrategy
		getWidgetFactory().createCLabel(composite, "Complete Strategy: ");
		
		completeStrategyCombo = new Combo(composite,SWT.DROP_DOWN);
		completeStrategyCombo.setItems(new String[]{"ALL","ANY"});
		completeStrategyCombo.setText("ALL");
		completeStrategyCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		completeStrategyCombo.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
//				System.out.println("-----haha------"+completeStrategyCombo.getText());
				((ActivityWrapper)modelWrapper).setCompleteStrategy(completeStrategyCombo.getText());
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
//				System.out.println("-----haha------"+completeStrategyCombo.getText());
				((ActivityWrapper)modelWrapper).setCompleteStrategy(completeStrategyCombo.getText());
			}
			
		});
	}
	
	@Override
	public void refresh() {
		super.refresh();
		String s = ((ActivityWrapper)modelWrapper).getCompleteStrategy();
		if (s==null || s.equals("ALL")){
			completeStrategyCombo.select(0);
		}else {
			completeStrategyCombo.select(1);
		}
		
//		this.completeStrategyCombo.setText(s);
	}
	
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Object obj = ((IStructuredSelection) selection).getFirstElement();
		if (obj instanceof AbstractEditPart) {

			editPart = (AbstractEditPart) obj;
			modelWrapper = (AbsWrapper) editPart.getModel();
		}
	}	
}
